package com.net.shop.car.manager.ui.personalcenter.dingdan;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.net.shop.car.manager.R;
import com.net.shop.car.manager.api.model.Member;
import com.net.shop.car.manager.ui.FullScreenDialog;
import com.net.shop.car.manager.utils.Constants.DingDanType;

public class DingdanDialog extends FullScreenDialog implements
		OnItemClickListener {
	private int[] dingNum;

	private String[] dingdanArrayStrings;
	private Member user;
	
	private int[] drawabls = new int[]{R.drawable.jifenduihuan,R.drawable.dangmianfu,
			R.drawable.xianshangfu,R.drawable.tuikuandingdan,R.drawable.yiwancheng,R.drawable.youka};

	public DingdanDialog(Context context,Member user) {
		super(context, R.style.Dialog_Fullscreen);
		this.user = user;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_dingdan);

		dingdanArrayStrings = activity.getResources().getStringArray(
				R.array.dingdan_name);
		initViews();
	}

	private void initViews() {
		initTitle(findViewById(R.id.dingdan_title), 
				activity.getResources().getString(R.string.personal_mybooking_dingdan));
		
		GridView dGridView = (GridView) findViewById(R.id.dingdan_gridview);

		dGridView.setAdapter(new BaseAdapter() {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				View currentView = convertView;
				if (currentView == null) {
					currentView = activity.inflater.inflate(
							R.layout.dingdan_griditem, parent, false);
				}
				((TextView) currentView.findViewById(R.id.dingdan_griditem_txt))
						.setText(dingdanArrayStrings[position]);
				((TextView) currentView
						.findViewById(R.id.dingdan_griditem_number))
						.setText(dingNum[position] + "");
				currentView.setBackgroundResource(drawabls[position]);
				return currentView;
			}

			@Override
			public long getItemId(int position) {
				// TODO Auto-generated method stub
				return 0;
			}

			@Override
			public Object getItem(int position) {
				// TODO Auto-generated method stub
				return null;
			}

			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return null == dingdanArrayStrings ? 0
						: dingdanArrayStrings.length;
			}
		});

		dGridView.setOnItemClickListener(this);
	}

	/**
	 * @param dingNum
	 *            the dingNum to set
	 */
	public void setDingNum(int[] dingNum) {
		this.dingNum = dingNum;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		DingDanType danType = DingDanType.getType(position);
		if(danType != null) {
			DingdanOrderDialog dialog = new DingdanOrderDialog(activity, danType,user);
			dialog.show();
		}
		
	}
}
